<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-30 17:38:06
 * @LastEditTime: 2024-02-27 22:45:32
 * @FilePath: \env-manager-pc\src\views\document\manager\create\zhi_wu\zhi.wu.fen.edit.vue
-->
<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { rulesFenZu } from './page'
import { ModelType } from '@/types/common'
import type { ModelOpen } from '@/types/page'
import { useInitChanWuForm } from '@/views/document/manager/create/createHelper'

const emits = defineEmits(['submit'])
const dialogVisible = ref(false)
const { formData, initForm } = useInitChanWuForm()
const formRef = ref<FormInstance>()
const btnLoading = ref(false)
const modelType = ref(ModelType.add)
function handleClose() {
  close()
}

const title = computed(() => {
  return formData.value.ObjID ? '编辑分组' : '新增分组'
})

function open(options: ModelOpen) {
  modelType.value = options.type
  if (options.data) {
    formData.value = { ...options.data }
  }
  dialogVisible.value = true
}

function doLoading(flag: boolean) {
  btnLoading.value = flag
}

function submit() {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      await emits('submit', formData.value, doLoading)
    }
  })
}

function close() {
  dialogVisible.value = false
  btnLoading.value = false
  initForm()
}

defineExpose({
  open,
  close,
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    append-to-body
    :title="title"
    width="30%"
    draggable
    @closed="handleClose"
  >
    <ElForm ref="formRef" :model="formData" label-width="180px" label-position="top" label-suffix="：" :rules="rulesFenZu">
      <ElRow :gutter="30">
        <ElCol :span="12">
          <ElFormItem label="分组名称" prop="Name">
            <ElInput v-model="formData.Name" />
          </ElFormItem>
        </ElCol>
        <ElCol :span="12">
          <ElFormItem label="至少同时运行数量" prop="RunNum">
            <ElInputNumber v-model="formData.RunNum" class="w-full" :controls="false" :min="0" />
          </ElFormItem>
        </ElCol>
        <ElCol :span="12">
          <ElFormItem label="安装位置" prop="Location">
            <ElInput v-model="formData.Location" />
          </ElFormItem>
        </ElCol>
      </ElRow>
    </ElForm>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submit">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
